package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.TranscriptSetV2;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.service.ITranscriptSetV2Service;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 当前成绩录入，考试名称/学期设置 前端控制器
 * </p>
 *
 * @author wangyuezhao
 * @since 2025-08-02
 */
@RestController
@RequestMapping("/api/transcriptSetV2")
public class TranscriptSetV2Controller {

    @Autowired
    private ITranscriptSetV2Service iTranscriptSetV2Service;

    @Autowired
    private IDictService iDictService;

    @Autowired
    IdWorker idWorker;

    @GetMapping()
    public Result findAll(@RequestParam Map reMap) {
        try {
            Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
            Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
            IPage<TranscriptSetV2> iPage = new Page<>(pageNum, pageSize);
            iTranscriptSetV2Service.page(iPage);
            return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id) {
        try {
            TranscriptSetV2 transcriptSet = iTranscriptSetV2Service.getById(id);
            return new Result(Code.OK, transcriptSet);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @GetMapping("/getTranscriptSetV2One")
    public Result getTranscriptSetV2One() {
        try {
            QueryWrapper<TranscriptSetV2> queryWrapper = new QueryWrapper<>();
            TranscriptSetV2 transcriptSet = iTranscriptSetV2Service.getOne(queryWrapper);
            return new Result(Code.OK, transcriptSet);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }


    @PostMapping()
    public Result save(@RequestBody TranscriptSetV2 transcriptSet) {
        try {
            boolean rs = iTranscriptSetV2Service.save(transcriptSet);
            return new Result(rs ? Code.OK : Code.ERROR);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @PutMapping()
    public Result update(@RequestBody TranscriptSetV2 transcriptSetV2) {
        try {
            transcriptSetV2.setCompareExamName(iDictService.getDictNameForNum(transcriptSetV2.getCompareExamNum(), 42));
            transcriptSetV2.setTeacherExamName(iDictService.getDictNameForNum(transcriptSetV2.getTeacherExamNum(), 42));
            boolean rs = iTranscriptSetV2Service.updateById(transcriptSetV2);
            return new Result(rs ? Code.OK : Code.ERROR);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id) {
        try {
            boolean rs = iTranscriptSetV2Service.removeById(id);
            return new Result(rs ? Code.OK : Code.ERROR);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody TranscriptSetV2 transcriptSet) {
        try {
            boolean rs = iTranscriptSetV2Service.saveOrUpdate(transcriptSet);
            return new Result(rs ? Code.OK : Code.ERROR);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

}
